package com.sanjiahantang.youjiarole.module.programmeprice.views.impl;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.goods.beans.GoodsAttrBean;
import com.sanjiahantang.youjiarole.module.goods.beans.GoodsBean;
import com.sanjiahantang.youjiarole.module.programmeprice.adapters.ProgramProjectAdapter;
import com.sanjiahantang.youjiarole.module.programmeprice.beans.ProgramBean;
import com.sanjiahantang.youjiarole.module.programmeprice.presenters.ProgramPricePresenter;
import com.sanjiahantang.youjiarole.module.programmeprice.views.ProgramPriceView;
import com.sanjiahantang.youjiarole.utils.FileUtils;


/**
 * 项目管理增项
 * Created by admin on 2017/10/25.
 */

public class ProgramChoiceInfoActivity extends BaseMvpActivity<ProgramPriceView, ProgramPricePresenter> implements ProgramPriceView, View.OnClickListener {

    private TextView title, txtsave, txtTotal;
    private View flLeft, flIcon3, yesAddress, noAddress, inVince, panelContract;
    private RecyclerView recyclerView;
    private CheckBox Contract;
    private BaseView.OnLoadListener listener;
    private String  addID, phone, designphone, designname;
    private int ppid;
    private CheckBox select_all_delete;
    private ProgramProjectAdapter adapter;

    @Override
    public ProgramPricePresenter initPresenter() {
        return new ProgramPricePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_program_choiceinfo);
        title = (TextView) findViewById(R.id.title);
        findViewById(R.id.fl_Left).setOnClickListener(this);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        recyclerView.setFocusableInTouchMode(false);
        recyclerView.setNestedScrollingEnabled(false);
        txtsave = (TextView) findViewById(R.id.txtsave);
        txtTotal = (TextView) findViewById(R.id.txtTotal);
        txtsave.setOnClickListener(this);
        title.setText("增项报价清单");
        ppid = getIntent().getIntExtra("PPID", 0);
        phone = getIntent().getStringExtra("customerphone");
        designname = getIntent().getStringExtra("designname");
        designphone = getIntent().getStringExtra("designphone");
        select_all_delete = (CheckBox) findViewById(R.id.select_all_delete);
        select_all_delete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isCheck) {
                if (isCheck == false) {
                    select_all_delete.setChecked(false);
                    presenter.prochangData($this, ProgramPriceView.SWITCH_SELECT_NONE);
                } else {
                    select_all_delete.setChecked(true);
                    presenter.prochangData($this, "all");
                }
            }
        });
    }

    @Override
    public void initData() {
        presenter.findPlaceOrder(this, ppid, 1);
        presenter.setOnLoadListener(new OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                Object[] param = (Object[]) content;
                switch ((int) param[0]) {
                    case CALLBACK_TYPE_RECYCLER_ADAPTER:
                        recyclerView.setAdapter((RecyclerView.Adapter) param[1]);
                        adapter = (ProgramProjectAdapter) param[1];
                        break;
                    case CALLBACK_TYPE_QUICK_ADAPTER:
                        txtTotal.setText(String.format("%.2f",(float)param[1]));
                        if ((float)param[1] > 0){
                            txtsave.setEnabled(true);
                            txtsave.setBackgroundColor(COLOR_PRIMARY);
                        }else{
                            txtsave.setEnabled(false);
                            txtsave.setBackgroundColor(Color.rgb(0xe8,0xe8,0xe8));
                        }
                        break;
                    case CALLBACK_TYPE_BOOLEAN:
                        if ((Boolean) param[1]==true){
                            select_all_delete.setBackgroundResource(R.drawable.i_radio_on);
                        }else {
                            select_all_delete.setBackgroundResource(R.drawable.i_radio_off);
                        }
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.txtsave://增项推送给客户
                try{
                    float sum = Float.valueOf(txtTotal.getText().toString());
                    if (sum > 0){
                        presenter.saveProjectPriceInfo($this, adapter.getData().getUserRequireId(),ppid);
                    }else{
                        Utils.showToast($this,"请选择增项");
                    }
                }catch (Exception e){
                    Utils.showToast($this,"请选择增项!");
                }



                break;

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        /**
         1.skuFaId
         2.planId方案id  long
         3.userRequireId 需求id long
         4.serviceName 服务名称 String
         5.servicePrice 单价 String
         6.serviceNum 数量 int
         7.serviceCount 合计 String
         8.typeId 1设计师 2客户3项目经理（分身份类型）
         9.serviceType 0报价清单1增项int
         10.isRecommend 是否推荐 0int
         11.mgiId 商品id long
         */
        if (ConfigValue.param != null && ConfigValue.param instanceof Object[]) {
            Object[] objects = (Object[]) ConfigValue.param;
            GoodsBean.Data obj = (GoodsBean.Data) objects[0];
            GoodsAttrBean.SkuData sku = (GoodsAttrBean.SkuData) objects[1];
            String number = (String) objects[2];
            switch (obj.getTypeGoodsId()) {
                case 4://施工
                    ProgramBean.Data.ServiceListBean sgbeans = new ProgramBean.Data.ServiceListBean();
                    sgbeans.setSkuFaId(sku.getSkuId());
                    sgbeans.setTypeId("3");
                    sgbeans.setServiceName(obj.getGoodsName());
                    sgbeans.setServicePrice("" + (sku.getSkuPrice() + obj.getBasePrice()));
                    sgbeans.setServiceNum(Integer.valueOf(number));
                    sgbeans.setServiceCount(String.valueOf((sku.getSkuPrice() + obj.getBasePrice())* Integer.valueOf(number)));
                    sgbeans.setTypeGoodsId(String.valueOf(obj.getTypeGoodsId() - 1));
                    sgbeans.setIsRecommend(1);
                    sgbeans.setServiceType(1);
                    sgbeans.setMgiId(obj.getMgiId());
                    sgbeans.setPhone(phone);
                    sgbeans.setRolePhone(designphone);
                    sgbeans.setDesignName(designname);
                    sgbeans.setUserRequireId(adapter.getData().getUserRequireId());
                    sgbeans.setPlanId(ppid);
                    adapter.addCell(sgbeans, 3);
                    break;
                case 1://主材
                    ProgramBean.Data.GoodsListBean zc_beans = new ProgramBean.Data.GoodsListBean();
                    zc_beans.setSkuFaId(sku.getSkuId());
                    zc_beans.setTypeId("3");
                    zc_beans.setGoodsName(obj.getGoodsName());
                    zc_beans.setGoodsPrice("" + (sku.getSkuPrice() + obj.getBasePrice()));
                    zc_beans.setGoodsNum(Integer.valueOf(number));
                    zc_beans.setGoodsCount(String.valueOf((sku.getSkuPrice() + obj.getBasePrice())* Integer.valueOf(number)));
                    zc_beans.setGoodsType(String.valueOf(obj.getTypeGoodsId() - 1));
                    zc_beans.setIsRecommend(1);
                    zc_beans.setServiceType(1);
                    zc_beans.setBrandType(sku.getMgpiValue());
                    zc_beans.setMgiId(obj.getMgiId());
                    zc_beans.setPhone(phone);
                    zc_beans.setSupplyName(obj.getSupplyName());
                    zc_beans.setSupplyId(obj.getSupplyId());
                    zc_beans.setImageUrl(FileUtils.rePath(obj.getUrl()));
                    zc_beans.setPlanId(ppid);
                    adapter.addCell(zc_beans, 4);
                    break;
                case 2://家具
                    ProgramBean.Data.GoodsListBean jjbeans = new ProgramBean.Data.GoodsListBean();
                    jjbeans.setSkuFaId(sku.getSkuId());
                    jjbeans.setTypeId("3");
                    jjbeans.setGoodsName(obj.getGoodsName());
                    jjbeans.setGoodsPrice("" + (sku.getSkuPrice() + obj.getBasePrice()));
                    jjbeans.setGoodsNum(Integer.valueOf(number));
                    jjbeans.setGoodsCount(String.valueOf((sku.getSkuPrice() + obj.getBasePrice())* Integer.valueOf(number)));
                    jjbeans.setGoodsType(String.valueOf(obj.getTypeGoodsId() - 1));
                    jjbeans.setIsRecommend(1);
                    jjbeans.setServiceType(1);
                    jjbeans.setBrandType(sku.getMgpiValue());
                    jjbeans.setPhone(phone);
                    jjbeans.setMgiId(obj.getMgiId());
                    jjbeans.setSupplyName(obj.getSupplyName());
                    jjbeans.setSupplyId(obj.getSupplyId());
                    jjbeans.setImageUrl(FileUtils.rePath(obj.getUrl()));
                    jjbeans.setPlanId(ppid);
                    adapter.addCell(jjbeans, 1);
                    break;
                case 3://配飾
                    ProgramBean.Data.GoodsListBean ps_beans = new ProgramBean.Data.GoodsListBean();
                    ps_beans.setSkuFaId(sku.getSkuId());
                    ps_beans.setTypeId("3");
                    ps_beans.setGoodsName(obj.getGoodsName());
                    ps_beans.setGoodsPrice("" + (sku.getSkuPrice() + obj.getBasePrice()));
                    ps_beans.setGoodsNum(Integer.valueOf(number));
                    ps_beans.setGoodsCount(String.valueOf((sku.getSkuPrice() + obj.getBasePrice())* Integer.valueOf(number)));
                    ps_beans.setGoodsType(String.valueOf(obj.getTypeGoodsId() - 1));
                    ps_beans.setIsRecommend(1);
                    ps_beans.setServiceType(1);
                    ps_beans.setBrandType(sku.getMgpiValue());
                    ps_beans.setPhone(phone);
                    ps_beans.setMgiId(obj.getMgiId());
                    ps_beans.setSupplyName(obj.getSupplyName());
                    ps_beans.setSupplyId(obj.getSupplyId());
                    ps_beans.setImageUrl(FileUtils.rePath(obj.getUrl()));
                    ps_beans.setPlanId(ppid);
                    adapter.addCell(ps_beans, 2);
                    break;
            }
            ConfigValue.param = null;
        }else{
            ConfigValue.param = null;
        }

    }

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

}
